import 'package:flutter/material.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'multi_state_widget.dart';

typedef RetryCallback = void Function();

class DefaultErrorWidget extends StatefulWidget {
  final Color backGroundColor;
  final MultiModel? model;
  final String errorIcon;
  final String errorText;
  final RetryCallback? onRetry;

  DefaultErrorWidget({
    Key? key,
    this.backGroundColor = Colours.color_background,
    this.model = MultiModel.normal,
    this.errorIcon = "images/ic_error.png",
    this.errorText = "网络异常 连接失败",
    this.onRetry,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _DefaultErrorWidgetState();
  }
}

class _DefaultErrorWidgetState extends State<DefaultErrorWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.backGroundColor,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(widget.model != null ? (widget.model == MultiModel.normal ? "images/ic_error.png" : "images/ic_error_tint.png") : widget.errorIcon),
            SizedBox(
              height: 5.w,
            ),
            Text(
              widget.errorText,
              style: TextStyle(
                color: Colours.color_333333_50,
                fontSize: 14.sp,
              ),
            ),
            SizedBox(
              height: 23.w,
            ),
            TextButton(
              onPressed: () {
                if (widget.onRetry != null) {
                  widget.onRetry!();
                }
              },
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colours.color_background),
                shape: MaterialStateProperty.all(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(5.w),
                    side: BorderSide(width: 1, color: Colours.color_theme),
                  ),
                ),
                padding: MaterialStateProperty.all(
                  EdgeInsets.symmetric(vertical: 5.w, horizontal: 25.w),
                ),
              ),
              child: Text(
                "重试",
                style: TextStyle(
                  color: Colours.color_theme,
                  fontSize: 14.sp,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
